package zj.it.bhne.gridengineeringsurvey.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.offline.MKOfflineMap;
import com.baidu.mapapi.map.offline.MKOfflineMapListener;
import com.baidu.mapapi.model.LatLng;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import zj.it.bhne.gridengineeringsurvey.BaseActivity;
import zj.it.bhne.gridengineeringsurvey.R;

public class OfflineMapActivity extends BaseActivity implements View.OnClickListener , MKOfflineMapListener {

    private ImageButton activity_ib_back;
    private TextView activity_tv_title;
    private TextView activity_tv_save;
    private MapView offline_mapView;
    private BaiduMap mBaiduMap;
    private static String PATH = "custom_config_dark.json";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_offline_map);
        initView();
    }

    private void initView() {
        activity_ib_back = findViewById(R.id.activity_ib_back);
        activity_tv_title = findViewById(R.id.activity_tv_title);
        activity_tv_save = findViewById(R.id.activity_tv_save);
        offline_mapView = findViewById(R.id.offline_mapView);
        activity_ib_back.setOnClickListener(this);
        activity_tv_title.setText("离线地图展示");
        activity_tv_save.setVisibility(View.INVISIBLE);
        Intent intent = getIntent();
        double lon = intent.getDoubleExtra("x",0);
        double lat = intent.getDoubleExtra("y",0);
        LatLng lng = new LatLng(lat,lon);
        mBaiduMap = offline_mapView.getMap();
        //调用离线地图，否则第二次进入会加载不到
        MKOfflineMap mkOfflineMap = new MKOfflineMap();
        mkOfflineMap.init(this);
        setMapCustomFile();
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
        MapStatus.Builder builder = new MapStatus.Builder();
        builder.target(lng).zoom(11.0f);
        mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
    }

    /**
     * 设置离线地图config文件路径
     */
    private void setMapCustomFile() {
        String str = Environment.getExternalStorageState() + "/BaiduMapSDKNew/vmp/";
        File file = new File(str);
        if (!(file.exists())) {
            try {
                new File(str).createNewFile();
                InputStream input = openFileInput(str + ".cfg");
                FileOutputStream output = new FileOutputStream(str);
                byte[] buffer = new byte[8192];
                int i;
                while ((i = input.read(buffer)) > 0) {
                    output.write(buffer, 0, i);
                }
                output.close();
                input.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_ib_back:
                finish();
                break;
        }
    }

    @Override
    public void onGetOfflineMapState(int i, int i1) {

    }
}
